import axios from 'axios';
import store from '../../store/index'
import router from '../../router/index'
import { Toast } from 'vant';

const host = 'http://www.ym718.com/api/'


const instance = axios.create({
  baseURL: '',
  timeout: 10000,
  withCredentials: false, // 允许携带cookie

});

instance.defaults.headers.get['Content-Type'] = 'application/json; charset=utf-8';
instance.defaults.headers.post['Accept-Language'] = 'zh-CN'
// instance.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';

instance.interceptors.response.use(response => {
  if (response.status === 200) {
    //console.log(response)
    if (response.data.code === 403) {
      sessionStorage.removeItem('userInfo');
      Toast({
        message: '登录过期，请重新登录',
        duration: 1000,
        forbidClick: true
      });
      setTimeout(() => {
        router.push('/login')
      }, 1000);
    } else {

      return Promise.resolve(response);
    }
  } else {
    return Promise.reject(response)
  }
},
  error => {
    console.log(error)
    let code = error.response.status;
    switch (code) {
      case 500:
        Toast({
          message: '服务器错误，请稍后重试！',
          duration: 1000,
          forbidClick: true
        });
        break;
      default:
        return false;
    }
    //console.log(error.response)
    return Promise.reject(error)
  })

export default {
  getCity(url) {
    return new Promise((cback, reject) => {
      instance.get(host + url).then(res => {
        console.log(res)
        if (res.data.code === 0) {
          cback(res.data)
        } else {
          reject(res.data)
        }
      })
    })
  },
  notLoggedIn(url) {
    return new Promise((cback, reject) => {
      instance.get(host + url).then(res => {
        console.log(res)
        if (res.data.code === 0) {
          cback(res.data.data)
        } else {
          reject(res.data.msg)
        }
      })
    })
  },
  get(url) {
    let userInfo = JSON.parse(store.state.userInfo)
    return new Promise((cback) => {
      if (userInfo) {
        instance.get(host + url, {
          headers: {
            Authorization: `bearer ${userInfo.access_token}`
          }
        }).then(res => {
          var status = res.status;
          var code = res.data.code;
          if (status === 200 && code === 0) {
            cback(res.data)
          } else {
            //console.log(res.data.msg)
          }
        })
      } else {
        Toast({
          message: '登录过期，请重新登录',
          duration: 1000,
          forbidClick: true
        });
        router.push('/login')
        return false
      }
    })
  },
  post(url, param) {
    let userInfo = JSON.parse(store.state.userInfo)
    return new Promise((cback) => {
      instance.post(host + url, param, {
        headers: {
          "Authorization": `bearer ${userInfo.access_token}`
        }
      }).then(msg => {
        cback(msg)
      })
    })
  },
  noJurisdictionPost(url, params) {
    return new Promise((cback) => {
      instance.post(host + url, params).then(msg => {
        cback(msg)
      })
    })
  },
  login(url, param) {
    return new Promise((cback) => {
      instance.post(host + url, param).then(res => {
        cback(res)
      })
    })
  },
  imageUpload(url, param) {
    let userInfo = JSON.parse(store.state.userInfo)
    return new Promise((cback) => {
      let config = {
        headers: {
          "Authorization": "bearer" + userInfo.access_token,
          "Content-Type": "form-data"
        }
      };
      instance.post(host + url, param, config).then(res => {
        cback(res)
        //console.log(res)
      })
    })
  },
  register(url, param) {
    return new Promise((cback) => {

      instance.post(host + url, param, {
        headers: {
          "Authorization": "Basic cGInOnBpZw==",
          "Accept-Language": "zh_CN"
        }
      }).then(res => {
        cback(res)
      })
    })
  }
}